package com.googlecode.lohr.parser.combinators.matcher;


import java.util.ArrayList;
import java.util.List;

import com.googlecode.lohr.parser.combinators.MatchResults;
import com.googlecode.lohr.parser.combinators.node.AbstractNode;
import com.googlecode.lohr.parser.combinators.node.FilterNode;

public class FilterMatcher extends Matcher
{
	private Matcher definition;

	public FilterMatcher define(Matcher definition)
	{
		this.definition = definition;
		return this;
	}

	public Matcher getDefinition() {
		return definition;
	}

	@Override
	public MatchResults match(MatchContext ctx, int start) 
	{
		List<AbstractNode> results = new ArrayList<AbstractNode>();

		MatchResults matchResults= ctx.doMatch(definition, start);
		if (!matchResults.success())
			return matchResults;

		for (AbstractNode result : matchResults.matches)
		{
			results.add(new FilterNode(this, result));
		}
		return new MatchResults(results);
	}

}
